Allow Flow initialization with required BaseModel fields via kwargs #3631
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Related Issues
Closes #3629
Changes
This PR fixes the initialization issue when using Pydantic BaseModel with required fields as Flow state by enabling kwargs to be passed during state creation.
What Changed
Modified
_create_initial_state()method signaturedict[str, Any] | None = NoneparameterUpdated
__init__()method_create_initial_state()_initialize_state(kwargs)call from init since kwargs are now handled during state creationFiles Changed
src/crewai/flow/flow.pyWhy This Solution
Additional Notes
The
_initialize_state()method remains unchanged and continues to be used bykickoff_async()for runtime state updates. This separation of concerns maintains clarity:_create_initial_state(): Creates the initial state during Flow instantiation_initialize_state(): Updates state during Flow execution (kickoff)Note
Enable passing kwargs to initialize Flow state (dict, BaseModel, FlowState), merging them during construction while preserving auto-generated id; add comprehensive tests.
_create_initial_state(kwargs)now accepts kwargs and merges them into state forFlowState,BaseModel, anddicttypes; prevents overriding auto-generatedid.__init__passeskwargsto_create_initial_state(removes separate_initialize_state(kwargs)during init).idin kwargs, and no-initial-state dict behavior.Written by Cursor Bugbot for commit 08b19e3. This will update automatically on new commits. Configure here.